﻿namespace CSharpCompiler
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Runtime.CompilerServices;
    using UnityEngine;

    public class DemoLoadScripts : MonoBehaviour
    {
        [CompilerGenerated]
        private static Action<object> <>f__am$cache5;
        public bool doStream;
        private List<string> loaded = new List<string>();
        private ScriptBundleLoader loader;
        public bool loadInBackground = true;
        private DeferredSynchronizeInvoke synchronizedInvoke;

        private void OnGUI()
        {
            string streamingAssetsPath = Application.streamingAssetsPath;
            int num = 0;
            foreach (string str2 in Directory.GetFiles(streamingAssetsPath, "*", SearchOption.AllDirectories))
            {
                if (!str2.EndsWith(".meta"))
                {
                    if (num > 20)
                    {
                        break;
                    }
                    num++;
                    string str3 = str2.Substring(streamingAssetsPath.Length);
                    if (this.loaded.Contains(str2))
                    {
                        GUILayout.Label("Loaded: " + str3, new GUILayoutOption[0]);
                    }
                    else if (GUILayout.Button("Load: " + str3, new GUILayoutOption[0]))
                    {
                        string[] fileSources = new string[] { str2 };
                        this.loader.LoadAndWatchScriptsBundle(fileSources);
                        this.loaded.Add(str2);
                    }
                }
            }
        }

        private void Start()
        {
            this.synchronizedInvoke = new DeferredSynchronizeInvoke();
            this.loader = new ScriptBundleLoader(this.synchronizedInvoke);
            this.loader.logWriter = new UnityLogTextWriter();
            this.loader.errorWriter = new UnityErrorTextWriter();
            this.loader.createInstance = delegate (System.Type t) {
                if (typeof(Component).IsAssignableFrom(t))
                {
                    return base.gameObject.AddComponent(t);
                }
                return Activator.CreateInstance(t);
            };
            if (<>f__am$cache5 == null)
            {
                <>f__am$cache5 = delegate (object instance) {
                    if (instance is Component)
                    {
                        UnityEngine.Object.Destroy(instance as Component);
                    }
                };
            }
            this.loader.destroyInstance = <>f__am$cache5;
        }

        private void Update()
        {
            this.synchronizedInvoke.ProcessQueue();
        }
    }
}

